﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Text;
using Models;
using IDAL;

namespace DAL
{
    public class AdminInfoDAL:IAdminInfo
    {
        public const string SQL_GET_ALL_ADMININFO = @"select * from AdminInfo";

        public DataBase DB = new DataBase();

        public List<AdminInfo> GetAllAdminInfo()
        {
            List<AdminInfo> list = new List<AdminInfo>();
            DB.DatabaseConnection();
            SqlDataReader reader = DB.GetdataRecords(SQL_GET_ALL_ADMININFO);
            while(reader.Read())
            {
                AdminInfo admininfo = new AdminInfo();
                ReadtoAdminInfo(reader, admininfo);
                list.Add(admininfo);
            }
            DB.CloseDB();
            return list;
        }

        public void ReadtoAdminInfo(SqlDataReader reader, AdminInfo admininfo)
        {
            try
            { admininfo.AdminID = Int32.Parse(reader["AdminID"].ToString()); }
            catch
            { }
            admininfo.UserName = reader["UserName"] is DBNull ? "" : reader["UserName"].ToString();
            admininfo.Password = reader["Password"] is DBNull ? "" : reader["Password"].ToString();
            
        }

        public int GetOneAdminInfo(AdminInfo admininfo)
        {
            int hasAdmin = -1;
            string getoneSQL = @"select * from AdminInfo where UserName ='"+admininfo.UserName
                +"' and Password = '"+admininfo.Password +"'";
            DB.DatabaseConnection();
            SqlDataReader reader = DB.GetdataRecords(getoneSQL);
            try
            {
                if (reader.Read())
                {
                    try
                    {
                        admininfo.AdminID = Int32.Parse(reader["AdminID"].ToString());
                    }
                    catch { }
                    hasAdmin = 1;
                }
            }
            catch
            {
                hasAdmin = -1;
            }
            DB.CloseDB();
            return hasAdmin;
        }

        public AdminInfo GetOneFromUid(int uid)
        {
            int isSuccess = -1;
            AdminInfo user = new AdminInfo();
            string sql = "SELECT * FROM AdminInfo WHERE AdminID = " + uid;
            DB.DatabaseConnection();
            SqlDataReader reader = DB.GetdataRecords(sql);
            while (reader.Read())
            {
                ReadtoAdminInfo(reader, user);
            }
            DB.CloseDB();
            return user;
        }
        public int InsertOneAdminInfo(AdminInfo admininfo)
        {
            int isSucess = -1;
            string insert_SQL = @"INSERT INTO AdminInfo([UserName],[Password]) 
                                    VALUES('" +admininfo.UserName + "','"+admininfo.Password+"')";
            isSucess = DB.UpdateRecords(insert_SQL);
            return isSucess;

        }
        public int UpdateOneAdminInfo(AdminInfo admininfo)
        {
            int isSucess = -1;
            string update_SQL = @"UPDATE AdminInfo
                                SET 
                                    [UserName] = '" + admininfo.UserName + @"',
                                    [Password] = '" + admininfo.Password + @"'
                                WHERE AdminID = " + admininfo.AdminID;
            isSucess = DB.UpdateRecords(update_SQL);
            return isSucess;
        }
        public int DeleteOneAdminInfo(int AdminID)
        {
            int isSucess = -1;
            string delete_SQL = @"DELETE FROM AdminInfo WHERE AdminID = " + AdminID;
            isSucess = DB.UpdateRecords(delete_SQL);
            return isSucess;
        }

        public int LogOutAdminInfo(string userName)
        {
            int isSucess = -1;
            string delete_SQL = @"DELETE FROM AdminInfo WHERE UserName = '" + userName+"'";
            isSucess = DB.UpdateRecords(delete_SQL);
            return isSucess;
        }

        public int GetCountFromUserName(string username)
        {
            int count = 0;
            string sql = "SELECT COUNT(*) AS C FROM AdminInfo WHERE UserName = '"+username+"'";
            DB.DatabaseConnection();
            SqlDataReader reader = DB.GetdataRecords(sql);
            while (reader.Read())
            {
                count = reader["C"] is DBNull ? 0 : Int32.Parse(reader["C"].ToString());
            }
            DB.CloseDB();
            return count;
        }
    }
}
